package phi.game.ball.core
{
	import Box2D.Dynamics.Contacts.b2Contact;
	import Box2D.Dynamics.b2ContactListener;
	
	import flash.net.getClassByAlias;
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;
	
	import phi.engine.PhiEngine;
	import phi.engine.actors.IActor;
	import phi.engine.actors.PolygonActor;
	import phi.engine.actors.SensorActor;
	import phi.game.ball.actors.BallActor;
	import phi.game.ball.actors.BuildingActor;
	
	public class ContactListner extends b2ContactListener
	{
		protected var engine :PhiEngine = PhiEngine.getInstance();
		
		public function ContactListner()
		{
			super();
		}
		
		override public function BeginContact(contact:b2Contact):void
		{
			var ball :BallActor = contact.GetFixtureB().GetBody().GetUserData() as BallActor;
			var actor :IActor = contact.GetFixtureA().GetBody().GetUserData() as IActor;
			var type :Object = getDefinitionByName( getQualifiedClassName( actor ) );
			
			if( !ball )
				return;
			
			switch( type )
			{
				case SensorActor:
				{
					engine.pause = true;
					break;
				}
					
				case BuildingActor:
				{
					ball.isJumping = false;
					ball.isFalling = false;
					
					if( contact.GetManifold().m_localPoint.y > 0 )
						ball.isFalling = true;
	
					break;
				}
			}
		}
		
		override public function EndContact(contact:b2Contact):void
		{
		}
	}
}